#include <iostream>

using namespace std;

const int N = 1e4 + 10;

int q[N], h, t;


int main()
{
	int n; cin >> n;
	int op;
	while(n--)
	{
		cin >> op;
		if(op == 1)//入队
		{
			int x; cin >> x;
			q[++t] = x;
		}
		else if(op == 2)//出队
		{
			if(h == t) cout << "ERR_CANNOT_POP" << endl;
			else ++h;
		}
		else if(op == 3)//查看队头元素
		{
			if(h == t) cout << "ERR_CANNOT_QUERY" << endl;
			else cout << q[h + 1] << endl;
		}
		else if(op == 4)//查看有效元素个数
		{
			cout << t - h << endl;
		}
	}

	return 0;
}
